#include <my_header.h>
#include"client.h"


int main(int argc, char *argv[]){
    //第一步先根据ip和端口号与服务端建立连接
    ARGS_CHECK(argc,3);
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    int ret=connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    printf("Connected to the server successfully.\n");
    //建立连接后先输入用户名，密码


    int epfd=epoll_create(1);
    struct epoll_event event;
    event.events=EPOLLIN;
    event.data.fd=sockfd;
    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);

    event.data.fd=STDIN_FILENO;
    epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);
    //将sockfd和stdin加入监听

    char buf[4096]={0};
    char username[50]={0};
    char password[50]={0};
    struct epoll_event events[10];
    while(1){
        bzero(buf,sizeof(buf));
        recvMsg(sockfd,buf);//先收对面用户名请求        
        printf("%s\n",buf);

        bzero(username,sizeof(username));
        read(STDIN_FILENO,username,sizeof(username));
        sendMsg(sockfd,username);//发送用户名

        bzero(buf,sizeof(buf));
        recvMsg(sockfd,buf);//收发密码请求
        printf("%s\n",buf);

        bzero(password,sizeof(password));
        read(STDIN_FILENO,password,sizeof(password));
        //密码加密后发送密码
        sendMsg(sockfd,password);

        bzero(buf,sizeof(buf));
        recvMsg(sockfd,buf);//判断是否密码正确
        printf("%s\n",buf);
        if(strcmp(buf,"Password is CORRECT!")==0)
        {break;
        }
    }
    int flag=-1;
    while(1)//第二步从输入缓冲区中读取命令
    {
        int num=epoll_wait(epfd,events,10,-1);
        for(int i=0;i<num;i++){
            if(events[i].data.fd==STDIN_FILENO){
                bzero(buf,sizeof(buf));
                read(STDIN_FILENO,buf,sizeof(buf));
                //对命令进行分析看是否符合格式
                flag=formVertify(buf);
                if(flag==-1)
                {
                    printf("Please re-enter due to formatting error.\n");//格式错误
                    continue;
                }
                if(flag==1){//发文件
                   ssize_t sret=sendMsg(sockfd,buf);
                    if(sret==0){
                        printf("The server has disconnected.\n");
                        close(sockfd);
                        close(epfd);
                        exit(1);
                    }
                    char p[10]={0};
                    char filename[1024]={0};//取出文件名
                    sscanf(buf,"%s %[^\n]",p,filename);
                    printf("before sendfile\n");
                     sret=sendFile(sockfd,filename);
                    printf("after sendMsg\n");
                    ssize_t ret=recvMsg(sockfd,buf);
                    if(ret==0){
                    printf("The server has disconnected.\n");
                    close(sockfd);
                    close(epfd);
                    exit(1);
                         }
                    printf("%s\n",buf);
                    if(sret==-1)continue;
                }
                else
                {//格式正确将整句话发过去        
                    ssize_t sret=sendMsg(sockfd,buf);
                    if(sret==0){
                        printf("The server has disconnected.\n");
                        close(sockfd);
                        close(epfd);
                        exit(1);
                    }
                }
            }
            else{
                /* if(flag==1){//发文件 */
                /*     char p[10]={0}; */
                /*     char filename[1024]={0};//取出文件名 */
                /*     sscanf(buf,"%s %[^\n]",p,filename); */
                /*     printf("before sendfile\n"); */
                /*     ssize_t sret=sendFile(sockfd,filename); */
                /*     printf("after sendMsg\n"); */
                /*     ssize_t ret=recvMsg(sockfd,buf); */
                /*     if(ret==0){ */
                /*     printf("The server has disconnected.\n"); */
                /*     close(sockfd); */
                /*     close(epfd); */
                /*     exit(1); */
                /* } */
                /*     printf("%s\n",buf); */
                /*     if(sret==-1)continue; */
                /* } */
                if(flag==2){//recvfile
                    ssize_t sret=recvFile(sockfd);
                    bzero(buf,sizeof(buf));
                ssize_t ret=recvMsg(sockfd,buf);
                     if(ret==0){
                    printf("The server has disconnected.\n");
                    close(sockfd);
                    close(epfd);
                    exit(1);
                }
                    printf("%s\n",buf);
                    if(sret==-1)continue;
                }
                //所有的业务都需要recv
                ssize_t sret=recvMsg(sockfd,buf);
                if(sret==0){
                    printf("The server has disconnected.\n");
                    close(sockfd);
                    close(epfd);
                    exit(1);
                }
                printf("%s\n",buf);
            }
        }
    }
}

